﻿namespace Nickel
{

    /// <summary>
    /// Comment
    /// </summary>
    public class Comment
    {

        /// <summary>
        /// Creates a comment instance
        /// </summary>
        public Comment()
        {
        }

        /// <summary>
        /// ID of author
        /// </summary>
        public string Author;

        /// <summary>
        /// Creation date time
        /// </summary>
        public string CreationDateTime;

        /// <summary>
        /// Comment ID
        /// </summary>
        public string ID;
        
        /// <summary>
        /// Modified date time
        /// </summary>
        public string ModifiedDateTime;

        /// <summary>
        /// Comment content
        /// </summary>
        public string Text;

        /// <summary>
        /// Comment form
        /// </summary>
        public string Form
        {

            get
            {

                // Start form
                string strForm = "<div id=\"" + (ID == null ? "" : ID) + 
                    Constants.SOURCE_FORM + "\">" +
                    "<h4" + (ID == null ? "" : " class=\"updateComment\"") + ">" + 
                    (ID == null ? Titles.COMMENT_NEW : Titles.COMMENT_UPDATE) + "</h4>" +
                    "<p>" + Instructions.COMMENT + "</p>" +
                    "<div class=\"formLine\">" + Constants.MARKUP_BAR_LITE.Replace("###id###", ID == null ? "" : ID) + "</div>" +
                    "<div><textarea name=\"" + 
                    (ID == null ? "" : ID) + Constants.INPUT_TEXT + "\" id=\"" +
                    (ID == null ? "" : ID) + Constants.INPUT_TEXT + "\">" + 
                    (Text == null ? "" : Text) + "</textarea>";

                // Add ID
                if (ID != null) 
                    strForm += "<input type=\"hidden\" id=\"" + 
                        Constants.INPUT_ID + "\" name=\"" + 
                        Constants.INPUT_ID + "\" value=\"" + ID + "\">";

                // Finish form
                strForm += "</div><div><input type=\"button\" name=\"" +
                    Constants.INPUT_SUBMIT + "\" id=\"" + 
                    Constants.INPUT_SUBMIT + 
                    "\" onclick=\"onClick('" + Constants.SOURCE_FORM + "', '" + Constants.TYPE_COMMENT + "'" + (ID == null ? "" : ", '" + ID + "'") + ");\" value=\"" + (ID == null ? Labels.SUBMIT_COMMENT_NEW : Labels.SUBMIT_COMMENT_UPDATE) + "\" />" +
                    "</div></div>";

                // Create error target
                if (ID != null) 
                    strForm += "<p id=\"" + ID + "formError\" name=\"" + ID + "formError\" class=\"formError\"></p>";

                // Return form
                return strForm;

            }

        }

        /// <summary>
        /// Create comment HTML
        /// </summary>
        /// <param name="data">Blog data</param>
        /// <returns>Comment HTML</returns>
        public string Html(Blog data, Author user, string strImage)
        {

            // Initialize return string
            string strComment = "<div id=\"" + ID + "\" class=\"comment\"><div class=\"commentHeader\">" + strImage;

            // Write article
            strComment += "<span class=\"tiny\">" + data.GetAuthorById(Author).Name +
                " / " + CreationDateTime + "</span>";

            // Add edit/delete links for signed in user or admin
            if (user != null && (user.ID == Author || user.Admin)) 
                strComment += "<span class=\"tiny\"> | </span>" +
                    "<a class=\"tiny\" style=\"color:#FFFFFF;\" onclick=\"onClick('" +
                    Constants.SOURCE_LINK + "', '" + Constants.TYPE_COMMENT + "', '" + ID + "');\">" +
                    "Edit</a><span class=\"tiny\"> | </span>" +
                    "<a class=\"tiny\" style=\"color:#FFFFFF;\" onclick=\"onClick('" +
                    Constants.SOURCE_LINK + "', '" + Constants.TYPE_COMMENT + "', '" + ID + "', true);\">" +
                    "Delete</a><span class=\"tiny\"> &nbsp; </span>" + 
                    "<span id=\"" + ID + "Error\" name=\"" + ID + "Error\" class=\"error\"></span>";

            // Add comment text
            strComment += "</div>" + Markup.ParseLite(StringHelper.RemoveHtml(Text));

            // Add edited time
            if (CreationDateTime != ModifiedDateTime) 
                strComment += "<p><span class=\"tiny\">" + 
                    "<em>Edited " + ModifiedDateTime + "</em></span></p>";

            // Return string
            return strComment + "</div>";

        }

    }

}
